import {
	getCurrentInstance,
	toRefs,
	reactive,
	onMounted,
	computed
} from 'vue'
import {useRouter,useRoute} from 'vue-router'
import {useStore} from 'vuex'
function elistModel() {
	const {proxy} = getCurrentInstance();
	
	const $router = useRouter();
	const $store = useStore();
	var data = reactive({
		elistData: [],
		//定义当前用户所在位置
		gps: {
			latitude: 28.242478,
			longitude: 112.946747
		},
		show_activity_state:{},
		count_all:computed(()=>{
			if($store.state.foodState){
				return $store.state.foodState;
			}
		})
	})
	//路由跳转  详情页
	function toDetails(items){
		// console.log(items)
		$router.push({
			path:'/detail/menu',
			query:{
				id:items.restaurant.id
			}
		})
	}
	
	// 改变列表选择的状态
	function showActivityFn(i){
		data.show_activity_state[i]=!data.show_activity_state[i]
		// console.log(data.show_activity_state)
	}
	//商家购买总数量
	function resIdCount(item){
		// console.log(item);
		
		//根据id 查询商家购买商品的数量
		var state = $store.state.foodState[item.restaurant.id]
		return state?state.count_all:0;
	}
	
	
	// 是否展开/隐藏活动列表
	function showActive(i){
	   var val=	data.show_activity_state[i];
	   val=val?'auto':'';
	   return{height:val}
	}

	
	//计算距离 (用户与商家的距离) 勾股定理 两点计算 x*x+y*y
	function distanceCalc(latitude, longitude) {
		//获取用户的经纬度
		var _latitude = data.gps.latitude;
		var _longitude = data.gps.longitude;

		var x, y, z;
		x = _latitude - latitude;
		y = _longitude - longitude;
		//计算两点之间的距离
		z = Math.sqrt(x * x + y * y) //得到用户和商家的距离

		return (z * 100).toFixed(2);
		// console.log(latitude,longitude)
	}
	//列表数据
	var itemData = computed(() => {
		var elistData = data.elistData;
		//判断数据存在否
		return elistData ? elistData : '';
	})
	
	//请求列表数据
	function getData(s,len,cb){
		// console.log(props.elistData)
		/* 获取列表数据(首页商家信息)
		   list?offset=0&len=8
		*/
		proxy.$axios('/list', {
			params: {
				offset: s,
				len: len
			}
		}).then(cb)		
	}
	
	//异步的 使用watch侦听 否则undefined
	onMounted(() => {
		var wH,dH,sT;
		var 
			dHtml = document.documentElement,
			dBody = document.body;
		var 
			offset=0,len=8,items;
		//滚动函数  判断是否到底
		function scrollFn(){
			console.log("开始滚动")
			wH = window.outerHeight;//窗口高度
			dH = dHtml.offsetHeight || dBody.offsetHeight;
			sT = dHtml.scrollTop || dBody.offsetTop;
			// console.log(wH,dH,sT);
			if(dH-wH<=sT){
				offset+=len;
				// console.log("到底了");
				getData(offset,len,res=>{
					items.push.apply(items,res.data.items)
				})
			}
		}
		window.addEventListener('scroll',scrollFn,false);
		
		getData(offset,len,res=>{
			data.elistData = res.data.items;
			items = data.elistData;
			console.log(res.data.items)
		})
	})
	return{
		distanceCalc,itemData,showActivityFn,showActive,toDetails,resIdCount
	}
}
export default elistModel;
